/*******************************************************************************
 * Licensed Materials - Property of IBM
 * (c) Copyright IBM Corporation 2005, 2008. All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:  Use,
 * duplication or disclosure restricted by GSA ADP Schedule
 * Contract with IBM Corp.
 *******************************************************************************/

package org.eclipsecon.dui.interpreter.internal;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.action.Action;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.FilteredResourcesSelectionDialog;

public class LoadAction extends Action {
	private final Shell fShell;
	private final ModelContainer fModelContainer;

	public LoadAction(ModelContainer modelContainer, Shell shell) {
		super("Load...", Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/open.gif"));
		fModelContainer= modelContainer;
		fShell= shell;
	}

	/*
	 * @see org.eclipse.jface.action.Action#run()
	 */
	@Override
	public void run() {
		FilteredResourcesSelectionDialog dialog= new FilteredResourcesSelectionDialog(fShell, false, ResourcesPlugin.getWorkspace().getRoot(), IResource.FILE);
		dialog.setTitle("Open UI Description");
		dialog.setInitialPattern("*.swt");
		dialog.setMessage("Please choose the UI description:");
		dialog.open();
		IFile file= (IFile) dialog.getFirstResult();

		if (file != null) {
			fModelContainer.setBindingPath(null);
			fModelContainer.setDataPath(null);

			URI modelPath= URI.createPlatformResourceURI(file.getFullPath().toString(), true);
			IPath script= Path.fromPortableString(file.getFullPath().removeFileExtension().toPortableString() + ".ext");

			new ReloadJob(fModelContainer, modelPath, script).schedule();
		}
	}
}